<?php

// @formatter:off
/**
 * @file WCViewInfoFactory.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: WCViewInfoFactory.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/mvc/WCViewInfoFactory.php $
 */
// @formatter:on

class WCViewInfoFactory extends WCFactory {
	//
	// Public class methods.
	public static function LoadInfo(&$item, $setManager = false) {
		$out = array();

		if($item instanceof WCRepresentation) {
			if($item->ok()) {
				foreach($item->fields() as $field) {
					if($field == "previews") {
						$out[$field] = explode("|", $item->{$field});
					} else {
						$out[$field] = $item->{$field};
					}
				}

				if(isset($out["directory"]) && $out["directory"] > 0 && method_exists($item, "directory")) {
					$dir = $item->directory();
					$out["directory_name"] = $dir->name();
				}
				if(isset($out["collection"]) && $out["collection"] > 0 && method_exists($item, "collection")) {
					$col = $item->collection();
					$out["collection_name"] = $col->name();
				}
			}
		} elseif($item instanceof WCRuleAction) {
			$out["id"] = $item->id();
			$out["name"] = $item->name();
			$out["type"] = $item->type();
			$out["mode"] = $item->mode();
			if($item->conflict()) {
				$out["conflict"] = true;
			}

			$rule = $item->rule();
			foreach(self::LoadInfo($rule) as $key => $value) {
				$out["rule_{$key}"] = $value;
			}
			foreach(self::LoadInterfacesInfo($rule) as $key => $value) {
				$out["rule_{$key}"] = $value;
			}
		}

		$out = array_merge($out, self::LoadSpecificInfo($item));

		if($setManager) {
			self::SetManager($out);
		}

		return $out;
	}
	public static function LoadInterfacesInfo(&$item, $setManager = false) {
		$out = array();

		if($item instanceof WCCountItems) {
			if($item->hasItems()) {
				$out["has_items"] = true;
				$out["count_items"] = $item->countItems();
			}

			if($item instanceof WCCountCollections && $item->hasCollections()) {
				$out["has_collections"] = true;
				$out["count_collections"] = $item->countCollections();
			}
			if($item instanceof WCCountComics && $item->hasComics()) {
				$out["has_comics"] = true;
				$out["count_comics"] = $item->countComics();
			}
			if($item instanceof WCCountImages && $item->hasImages()) {
				$out["has_images"] = true;
				$out["count_images"] = $item->countImages();
			}
			if($item instanceof WCCountDirectories && $item->hasDirectories()) {
				$out["has_directories"] = true;
				$out["count_directories"] = $item->countDirectories();
			}
		}

		if($item instanceof WCPreviews && $item->hasPreview()) {
			$out["has_preview"] = true;
			$out["preview"] = $item->preview();

			if($item->hasPreviews()) {
				$out["has_previews"] = true;
				$out["previews"] = $item->previews();
			}
		}

		if($setManager) {
			self::SetManager($out);
		}

		return $out;
	}
	//
	// Protected class methods.
	protected static function LoadSpecificInfo(&$item) {
		$out = array();

		if($item instanceof WCImage) {
			$out["pubic_path"] = $item->publicPath();
			$out["pubic_uri"] = $item->publicUri();
		}

		return $out;
	}
	protected static function SetManager(&$info) {
		$manager = WCViewManager::I();
		foreach($info as $key => $value) {
			eval("\$manager->info_{$key} = \$value;");
		}
	}
}
